package com.indusfo.spc.client.controller;

import com.indusfo.spc.client.service.ClientClasseService;
import com.indusfo.spc.client.service.ClientClasseWorkService;
import com.indusfo.spc.client.service.ClientStationService;
import com.indusfo.spc.pojo.ClasseWork;
import com.indusfo.spc.pojo.Station;
import com.indusfo.spc.pojo.User;
import com.indusfo.spc.service.RoleStationService;
import com.indusfo.spc.service.TokenService;
import com.indusfo.spc.service.UserService;
import com.indusfo.spc.untlis.StringUtils;
import com.indusfo.spc.vo.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author chenm
 * @create 2019-12-03 9:51
 */
@RestController
@RequestMapping("/client")
public class ClientLogin {

    @Autowired
    private UserService userService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private RoleStationService roleStationService;

    @Autowired
    private ClientStationService clientStationService;

    @Autowired
    private ClientClasseService clientClasseService;

  @Autowired
  private ClientClasseWorkService clientClasseWorkService;

    @PostMapping(value = "/login")
    public JSONObject login( @RequestBody User user, HttpServletResponse response){
        try{
          if(StringUtils.isEmpty(user.getUserCode())){
            return JSONObject.build(JSONObject.ERROR,"登录账号不能为空");
          }
          if(StringUtils.isEmpty(user.getUserPwd())){
            return JSONObject.build(JSONObject.ERROR,"用户密码不能为空");
          }
          if(null == user.getStationId()){
            return JSONObject.build(JSONObject.ERROR,"工位信息不能为空");
          }
          User dataUser = userService.selectUserBycode(user.getUserCode());
          if(null == dataUser){
            return JSONObject.build(JSONObject.ERROR,"登录失败,账号不存在|密码错误");
          }
          ClasseWork classeWork = clientClasseWorkService.selectByProductId(user.getProductlineId());
            if(null == classeWork ){
                return JSONObject.build(JSONObject.ERROR,"该产线的班次未开工");
            }
          if(!dataUser.getUserPwd().equals(userService.encryptPassword(user.getUserCode(),user.getUserPwd(),dataUser.getSalt()))){
            return JSONObject.build(JSONObject.ERROR,"登录失败,账号不存在|密码错误");
          }else{
            if(dataUser.getUserId() != 1){
              List<Integer> stationIdList = roleStationService.selectStationIdByUserId(dataUser.getUserId());
              if(!stationIdList.contains(user.getStationId())){
                return JSONObject.build(JSONObject.ERROR,"账号没有此工位权限");
              }else{
                Station station = clientStationService.selectByStationId(user.getStationId());
                if(station.getDataState().equals(3)){
                  return JSONObject.build(JSONObject.ERROR,"工位已被禁用");
                }
                dataUser.setStationName(station.getStationName());
                dataUser.setStationId(user.getStationId());
              }
            }
            dataUser.setClasse(clientClasseService.getById(classeWork.getClasseId()));
            String token = tokenService.getToken(dataUser);
            dataUser.setToken(token);
            Cookie cookie = new Cookie("token", token);
            cookie.setPath("/");
            response.addCookie(cookie);
            return JSONObject.build(JSONObject.SUCCESS,"登录成功",dataUser);
          }
        }catch (Exception e){
          e.printStackTrace();
          return JSONObject.build(JSONObject.ERROR,"系统出错");
        }
    }
}
